package com.chenyue.cm.match.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.live.domain.JdLiveRecord;
import com.chenyue.cm.live.service.JdLiveRecordService;
import com.chenyue.cm.live.vo.param.SetMatchLiveUser;
import com.chenyue.cm.match.domain.*;
import com.chenyue.cm.match.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chenyue-cm
 * @date 2021-10-23
 */
@RestController
@RequestMapping("/formatLiveUser")
public class FormatLiveUserController {

    private final FormatLiveUserService formatLiveUserService;
    private final JdLiveRecordService recordService;
    private final JdMatchLiveUserService jdMatchLiveUserService;
    private final JdLiveRecordService jdLiveRecordService;
    private final JdMatchFormatMemberService jdMatchFormatMemberService;
    private final JdMatchSignService jdMatchSignService;
    private final JdMatchWhiteApplyService jdMatchWhiteApplyService;
    private final JdMatchFormatService jdMatchFormatService;

    public FormatLiveUserController(FormatLiveUserService formatLiveUserService, JdLiveRecordService recordService, JdMatchLiveUserService jdMatchLiveUserService, JdLiveRecordService jdLiveRecordService, JdMatchFormatMemberService jdMatchFormatMemberService, JdMatchSignService jdMatchSignService, JdMatchWhiteApplyService jdMatchWhiteApplyService, JdMatchFormatService jdMatchFormatService) {
        this.formatLiveUserService = formatLiveUserService;
        this.recordService = recordService;
        this.jdMatchLiveUserService = jdMatchLiveUserService;
        this.jdLiveRecordService = jdLiveRecordService;
        this.jdMatchFormatMemberService = jdMatchFormatMemberService;
        this.jdMatchSignService = jdMatchSignService;
        this.jdMatchWhiteApplyService = jdMatchWhiteApplyService;
        this.jdMatchFormatService = jdMatchFormatService;
    }


    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<FormatLiveUser> result = formatLiveUserService.getPageBean(pageInfo, FormatLiveUser.class);
        return Success.success(result);
    }

    @RequestMapping("/getFormatLiveUserList")
    public Success getFormatLiveUserList(HttpServletRequest request) {
        String formatId = request.getParameter("formatId");
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        LambdaQueryWrapper<JdMatchLiveUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdMatchLiveUser::getFormatId, formatId);
        List<JdMatchLiveUser> list = jdMatchLiveUserService.list(wrapper);
        //封装返回结果
        PageInfo<JdMatchLiveUser> result = new PageInfo<>(list);
        return Success.success(result);
    }

    @RequestMapping("/formatMemberList")
    @ApiOperation("可分配参赛员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "赛程id", name = "formatId", dataType = "string", required = true),
    })
    public Success formatMemberList(HttpServletRequest request) {
        String formatId = request.getParameter("formatId");
        String liveRecordId = request.getParameter("liveRecordId");
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        LambdaQueryWrapper<FormatLiveUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormatLiveUser::getFormatId, formatId);
        if(StringUtils.isNotBlank(liveRecordId)){
            wrapper.eq(FormatLiveUser::getLiveRecordId, liveRecordId);
        }
        wrapper.orderByAsc(FormatLiveUser::getUserName);
        List<FormatLiveUser> list = formatLiveUserService.list(wrapper);
        //封装返回结果
        PageInfo<FormatLiveUser> result = new PageInfo<>(list);
        return Success.success(result);
    }

    @RequestMapping("/formatMemberListGroup")
    @ApiOperation("可分配参赛员列表-团队")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "赛程id", name = "formatId", dataType = "string", required = true),
    })
    public Success formatMemberListGroup(HttpServletRequest request) {
        String formatId = request.getParameter("formatId");
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        List<FormatLiveUser> list = formatLiveUserService.lambdaQuery()
                .eq(FormatLiveUser::getFormatId, formatId).groupBy(FormatLiveUser::getGroupId).list();
        //封装返回结果
        PageInfo<FormatLiveUser> result = new PageInfo<>(list);
        return Success.success(result);
    }

    /**
     * 通过groupId formatId                    jd_match_format_member
     * 通过groupId formatId  live_record_id    jd_match_live_user
     * @param request
     * @return
     */
    @RequestMapping("/delFormatMemberListGroup")
    @ApiOperation("删除团队列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "赛程id", name = "formatId", dataType = "string", required = true),
            @ApiImplicitParam(value = "组别id", name = "groupId", dataType = "string", required = true),
            @ApiImplicitParam(value = "赛事id", name = "matchId", dataType = "string", required = true),
            @ApiImplicitParam(value = "直播id", name = "liveRecordId", dataType = "string", required = false),
    })
    public Success delFormatMemberListGroup(HttpServletRequest request) {
        String formatId = request.getParameter("formatId");
        String groupId = request.getParameter("groupId");
        String liveRecordId = request.getParameter("liveRecordId");
        String matchId = request.getParameter("matchId");
        //删除jd_match_format_member
        LambdaQueryWrapper<JdMatchFormatMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdMatchFormatMember::getFormatId, formatId);
        wrapper.eq(JdMatchFormatMember::getGroupId, groupId);
        boolean success = jdMatchFormatMemberService.remove(wrapper);
        //删除jd_match_live_user
        LambdaQueryWrapper<JdMatchLiveUser> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(JdMatchLiveUser::getFormatId, formatId);
        wrapper2.eq(JdMatchLiveUser::getGroupId, groupId);
        if(StringUtils.isNotBlank(liveRecordId)){
            wrapper2.eq(JdMatchLiveUser::getLiveRecordId, liveRecordId);
        }
        boolean success2 = jdMatchLiveUserService.remove(wrapper2);
        //如果是白名单用户
        boolean success3 = true;
        boolean success4 = true;
        if(StringUtils.isNotBlank(groupId) && Integer.parseInt(groupId) == 0){
            //通过match_id，group_id删除jd_match_sign
            LambdaQueryWrapper<JdMatchSign> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(JdMatchSign::getMatchId, matchId);
            wrapper3.eq(JdMatchSign::getGroupId, groupId);
            success3 = jdMatchSignService.remove(wrapper3);

            //通过match_id删除jd_match_white_apply
            LambdaQueryWrapper<JdMatchWhiteApply> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(JdMatchWhiteApply::getMatchId, matchId);
            success4 = jdMatchWhiteApplyService.remove(wrapper4);
        }
        return success && success2 && success3 && success4? Success.success() : Success.error();
    }

    @RequestMapping("/delFormatMemberList")
    @ApiOperation("删除赛程用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "赛程id", name = "formatId", dataType = "string", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "string", required = true),
            @ApiImplicitParam(value = "赛事id", name = "matchId", dataType = "string", required = true),
            @ApiImplicitParam(value = "直播id", name = "liveRecordId", dataType = "string", required = false),
    })
    public Success delFormatMemberList(HttpServletRequest request) {
        String formatId = request.getParameter("formatId");
        String matchId = request.getParameter("matchId");
        JdMatchFormat jdMatchFormat = jdMatchFormatService.selectByPrimaryKey(formatId);
        String userId = request.getParameter("userId");
        String liveRecordId = request.getParameter("liveRecordId");
        //删除jd_match_format_member
        LambdaQueryWrapper<JdMatchFormatMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdMatchFormatMember::getFormatId, formatId);
        wrapper.eq(JdMatchFormatMember::getUserId, userId);
        boolean success = jdMatchFormatMemberService.remove(wrapper);
        //删除jd_match_live_user
        LambdaQueryWrapper<JdMatchLiveUser> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(JdMatchLiveUser::getFormatId, formatId);
        wrapper2.eq(JdMatchLiveUser::getUserId, userId);
        if(StringUtils.isNotBlank(liveRecordId)){
            wrapper2.eq(JdMatchLiveUser::getLiveRecordId, liveRecordId);
        }
        boolean success2 = jdMatchLiveUserService.remove(wrapper2);
        //如果是决赛，才有白名单用户
        boolean success3 = true;
        boolean success4 = true;
        if(jdMatchFormat != null && jdMatchFormat.getArrangeOrder() == 3){
//            //通过match_id，group_id删除jd_match_sign
            LambdaQueryWrapper<JdMatchSign> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(JdMatchSign::getMatchId, matchId);
            wrapper3.eq(JdMatchSign::getUserId, userId);
            wrapper3.eq(JdMatchSign::getRemark, "白名单用户");
            success3 = jdMatchSignService.remove(wrapper3);

//            //通过match_id userId 删除jd_match_white_apply
            LambdaQueryWrapper<JdMatchWhiteApply> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(JdMatchWhiteApply::getMatchId, matchId);
            wrapper4.eq(JdMatchWhiteApply::getUserId, userId);
            success4 = jdMatchWhiteApplyService.remove(wrapper4);
        }
        return success && success2 && success3 && success4? Success.success() : Success.error();
    }

    @RequestMapping("/formatMemberListByGroupId")
    @ApiOperation("查看团队下的人员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "赛程id", name = "formatId", dataType = "string", required = true),
            @ApiImplicitParam(value = "团队id", name = "groupId", dataType = "string", required = true),
    })
    public Success formatMemberListByGroupId(HttpServletRequest request) {
        String formatId = request.getParameter("formatId");
        String groupId = request.getParameter("groupId");
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        List<FormatLiveUser> list = formatLiveUserService.lambdaQuery()
                .eq(FormatLiveUser::getFormatId, formatId)
                .eq(FormatLiveUser::getGroupId, groupId).list();
        //封装返回结果
        PageInfo<FormatLiveUser> result = new PageInfo<>(list);
        return Success.success(result);
    }



    @RequestMapping("/setMatchLiveUser")
    @ApiOperation("分配参赛员")
    public Success<String> setMatchLiveUser(
            SetMatchLiveUser matchLiveUser
    ) {
        recordService.setMatchLiveUser(matchLiveUser);
        return Success.success();
    }

    @RequestMapping("/getFormatLiveRecordList")
    public Success getFormatLiveRecordList(HttpServletRequest request) {
        String formatId = request.getParameter("formatId");
        //设置分页信息
        LambdaQueryWrapper<JdLiveRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdLiveRecord::getArrangeId, formatId);
        List<JdLiveRecord> list = jdLiveRecordService.list(wrapper);
        return Success.success(list);
    }

}
